package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Module;
import com.itheima.service.system.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo<Module> pageInfo = moduleService.findAll(page, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "/system/module/module-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus", moduleList);
        return "/system/module/module-add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        Module module = moduleService.findById(id);
        request.setAttribute("module", module);
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus", moduleList);
        return "/system/module/module-update";
    }

    @RequestMapping("/edit")
    public String edit(Module module) {
        if (StringUtils.isEmpty(module.getId())) {
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        } else {
            moduleService.update(module);
        }
        //跳转页面
        return "redirect:/system/module/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }
}
